using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace GameOfLife
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        GameOfLifeRules gameOflife;
        const int boardsize = 100;
        Texture2D gridImage;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            gameOflife = new GameOfLifeRules(boardsize, boardsize);
            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 1000;
            graphics.ToggleFullScreen();
            graphics.ApplyChanges();
            IsMouseVisible = true;
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            gridImage = Content.Load<Texture2D>("square");
            initGun();
            initGun2();
        }

        void initGun()
        {
            gameOflife.initializeLifeInCell(5, 6);
            gameOflife.initializeLifeInCell(5, 7);
            gameOflife.initializeLifeInCell(6, 6);
            gameOflife.initializeLifeInCell(6, 7);//box 1

            gameOflife.initializeLifeInCell(39, 4);
            gameOflife.initializeLifeInCell(39, 5);
            gameOflife.initializeLifeInCell(40, 4);
            gameOflife.initializeLifeInCell(40, 5);//box 2

            gameOflife.initializeLifeInCell(13, 7);
            gameOflife.initializeLifeInCell(13, 8);
            gameOflife.initializeLifeInCell(14, 8);
            gameOflife.initializeLifeInCell(14, 6);
            gameOflife.initializeLifeInCell(15, 6);
            gameOflife.initializeLifeInCell(15, 7);//shape 1

            gameOflife.initializeLifeInCell(27, 5);
            gameOflife.initializeLifeInCell(27, 6);
            gameOflife.initializeLifeInCell(28, 6);
            gameOflife.initializeLifeInCell(28, 4);
            gameOflife.initializeLifeInCell(29, 4);
            gameOflife.initializeLifeInCell(29, 5);//shape 2

            gameOflife.initializeLifeInCell(21, 8);
            gameOflife.initializeLifeInCell(22, 8);
            gameOflife.initializeLifeInCell(21, 9);
            gameOflife.initializeLifeInCell(21, 10);
            gameOflife.initializeLifeInCell(23, 9);//middle shape

            gameOflife.initializeLifeInCell(40, 11);
            gameOflife.initializeLifeInCell(41, 11);
            gameOflife.initializeLifeInCell(40, 12);
            gameOflife.initializeLifeInCell(40, 13);
            gameOflife.initializeLifeInCell(42, 12);//right shape

            gameOflife.initializeLifeInCell(29, 16);
            gameOflife.initializeLifeInCell(30, 16);
            gameOflife.initializeLifeInCell(31, 16);
            gameOflife.initializeLifeInCell(29, 17);
            gameOflife.initializeLifeInCell(30, 18);//bottom shape
        }

        void initGun2()
        {
            gameOflife.initializeLifeInCell(52, 3);
            gameOflife.initializeLifeInCell(52, 4);
            gameOflife.initializeLifeInCell(53, 3);
            gameOflife.initializeLifeInCell(53, 4);//box 1

            gameOflife.initializeLifeInCell(86, 5);
            gameOflife.initializeLifeInCell(86, 6);
            gameOflife.initializeLifeInCell(87, 5);
            gameOflife.initializeLifeInCell(87, 6);//box 2

            gameOflife.initializeLifeInCell(63, 4);
            gameOflife.initializeLifeInCell(63, 3);
            gameOflife.initializeLifeInCell(64, 5);
            gameOflife.initializeLifeInCell(64, 3);
            gameOflife.initializeLifeInCell(65, 5);
            gameOflife.initializeLifeInCell(65, 4);//Left Shape

            gameOflife.initializeLifeInCell(77, 6);
            gameOflife.initializeLifeInCell(77, 5);
            gameOflife.initializeLifeInCell(78, 7);
            gameOflife.initializeLifeInCell(78, 5);
            gameOflife.initializeLifeInCell(79, 7);
            gameOflife.initializeLifeInCell(79, 6);//Right Shape

            gameOflife.initializeLifeInCell(71, 7);
            gameOflife.initializeLifeInCell(70, 7);
            gameOflife.initializeLifeInCell(71, 8);
            gameOflife.initializeLifeInCell(71, 9);
            gameOflife.initializeLifeInCell(69, 8);//middle shape

            gameOflife.initializeLifeInCell(52, 10);
            gameOflife.initializeLifeInCell(51, 10);
            gameOflife.initializeLifeInCell(52, 11);
            gameOflife.initializeLifeInCell(52, 12);
            gameOflife.initializeLifeInCell(50, 11);//Bottom right shape

            gameOflife.initializeLifeInCell(63, 15);
            gameOflife.initializeLifeInCell(62, 15);
            gameOflife.initializeLifeInCell(61, 15);
            gameOflife.initializeLifeInCell(63, 16);
            gameOflife.initializeLifeInCell(62, 17);//bottom shape
        }

        protected override void UnloadContent()
        {
        }


        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            MouseState currentMouseState = Mouse.GetState();
            if (currentMouseState.LeftButton == ButtonState.Pressed)
                try
                {
                    gameOflife.initializeLifeInCell(currentMouseState.X / 10, currentMouseState.Y / 10);
                }
                catch { }
            if (currentMouseState.RightButton == ButtonState.Pressed)
                gameOflife.nextTimeStep();
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            bool[,] currentBoard = gameOflife.getGameBoard();
            spriteBatch.Begin();
            for (int x = 0; x < boardsize; x++)
                for (int y = 0; y < boardsize; y++)
                    if (currentBoard[x, y])
                        spriteBatch.Draw(gridImage, new Vector2(x * 10, y * 10), null, Color.Red, 0.0f, new Vector2(0, 0), 2.0f, SpriteEffects.None, 0);
                    else
                        spriteBatch.Draw(gridImage, new Vector2(x * 10, y * 10), null, Color.Blue, 0.0f, new Vector2(0, 0), 2.0f, SpriteEffects.None, 0);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
